from typing import Optional, Any, Dict, Union, TypeVar
from flask import jsonify, Response, current_app
from datetime import datetime

T = TypeVar('T')

class APIResponse:
    """API响应工具类"""
    
    @staticmethod
    def success(data=None, message=None):
        """成功响应"""
        response = {
            'status': 'success'
        }
        if data is not None:
            response['data'] = data
        if message:
            response['message'] = message
        return jsonify(response)

    @staticmethod
    def error(message=None, status_code=400, errors=None):
        """错误响应"""
        response = {
            'status': 'error',
            'message': message or 'An error occurred'
        }
        if errors:
            response['errors'] = errors
        return jsonify(response), status_code 

    @staticmethod
    def not_found(message=None):
        """404 响应"""
        return APIResponse.error(
            message=message or 'Resource not found',
            status_code=404
        )

    @staticmethod
    def server_error(message=None):
        """500 响应"""
        return APIResponse.error(
            message=message or 'Internal server error',
            status_code=500
        )

    @staticmethod
    def validation_error(message=None, errors=None):
        """验证错误响应"""
        return APIResponse.error(
            message=message or 'Validation failed',
            status_code=400,
            errors=errors
        ) 